package com.wdz.controller;


import com.jcraft.jsch.SftpException;
import com.wdz.http.Response;
//import com.wdz.tenxunyuncos.COSService;
import com.wdz.utils.FileUtil;
import com.wdz.utils.MultipartFileToFile;
import com.wdz.utils.tenxunyuncos.COSService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api")
public class UploadController {

    @Value("${app_file.basePath}")
    private String basePath;
    @Value("${app_file.host}")
    private String host;
    private String ym = "https://wdz-1258450135.cos.ap-shanghai.myqcloud.com";

    /**
     * 普通上传文件接口
     * @param file
     * @param directory
     * @return
     * @throws IOException
     * @throws SftpException
     */
    @PostMapping("/upload")
    public Response upload(MultipartFile file,String directory) throws Exception {
       // MultipartFile file = imgFile;
        //String filePath = "https://212.64.22.159//usr/local/cfs/";
        //设置上传服务器的路径 要保证有loads这个目录
        /*if(directory == null){
            directory = "";
        }
        System.out.println(file.getContentType());
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getContentType());
        File file1 = null;
        file1=File.createTempFile("tmp", null);
        file.transferTo(file1);
        file1.deleteOnExit();
        FileInputStream in=new FileInputStream(file1);
        String host = "212.64.22.159";
        int port = 22;
        SFTPUtil sftpUtil = new SFTPUtil("root","!wuchuan1258",host,port);
        sftpUtil.login();
        String fileName = UUID.randomUUID().toString().replaceAll("-", "")+file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
        String basePath = "/usr/local/cfs";
        sftpUtil.upload(basePath,"/"+directory,fileName,in);
        sftpUtil.logout();
        StringBuffer url = new StringBuffer();
        url.append(host);
        url.append(":8081/");
        url.append(directory);
        url.append(fileName);*/
        /*StringBuffer filePath = new StringBuffer(basePath);
        if(directory != null){
            filePath.append(directory);
            filePath.append("/");
        }*/
        //根据uuid生成文件名
        String fileName = FileUtil.makeFileName(file);
        StringBuilder str = new StringBuilder();
        if(directory != null){
            str.append("/");
            str.append(directory);
            str.append("/");
        }
        str.append(fileName);
            COSService.upload(null,MultipartFileToFile.multipartFileToFile(file),"wdz-1258450135",str.toString());

        /*if(FileUtil.uploadFile(file,filePath.toString(),fileName)) {
            StringBuffer url = new StringBuffer(host);
            if(directory != null){
                url.append(directory);
                url.append("/");
            }
            url.append(fileName);
            return Response.success(url);
        }*/
        StringBuilder url = new StringBuilder(ym);
        url.append(str);
        return Response.success(url);
    }

    @PostMapping("/makeHtml")
    public Response makeHtml(@RequestParam String htmlValue,String directory,String qfileName){
        StringBuffer filePath = new StringBuffer(basePath);
        if(directory != null){
            filePath.append(directory);
            filePath.append("/");
        }
        //根据uuid生成文件名
        String fileName;
        if(StringUtils.isEmpty(qfileName)) {
            fileName = FileUtil.makeFileName();
        }else{
            fileName = qfileName;
        }
        if(FileUtil.strToMakeHtml(filePath.toString(),htmlValue,fileName)){
            StringBuffer url = new StringBuffer(host);
            if(directory != null){
                url.append(directory);
                url.append("/");
            }
            url.append(fileName);
            url.append(".html");
            return Response.success(url);
        }
        return Response.error();
    }

}
